/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"

#include "LED.h"
#include "usart.h"

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}


/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */

extern void xPortSysTickHandler(void);
void SysTick_Handler(void)
{ 


}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 


extern DMA_InitTypeDef DMA_InitStructure;

void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)//这个函数专门为了中断设计的，不仅检查SR还回去检测CR1寄存器中该位是否使能，标志位不要检测错了
  {
    //1. 清除空闲中断标志位 软件上先读USART_SR 再读USART_DR
    USART_GetFlagStatus(USART1,USART_FLAG_IDLE);   //这个函数只检测SR寄存器的IDLE位
    //这个是读的SR寄存器的IDLE位，该位由硬件置位，当检测到总线空闲并且CR1中IDLE中断使能之后,会触发中断
    USART_ReceiveData(USART1);
    // USART_ClearITPendingBit(USART1,USART_IT_IDLE);
    u1_printf("into interrupt\r\n");
   

    //2. 空闲终端开的dma normal模式
    uint16_t dataLength = DMA_GetCurrDataCounter(DMA1_Channel5); // 获取当次获取的数据长度
    U1_CB.U_Data_counter+=(U1_RxMaxSize-dataLength); // 更新接收数据计数
    U1_CB.U_Data_IN->end = &U1_RxBuff[U1_CB.U_Data_counter-1]; // 更新接收指针的末尾地址
    U1_CB.U_Data_IN++;
    if(U1_CB.U_Data_IN==U1_CB.U_Data_end) // 如果接收指针对已经指向接收指针对数组的末尾 回卷
    {
      U1_CB.U_Data_IN=&U1_CB.U1_Rx_Ptr_Buff[0];//这个数组只是存放每次数据接收的量和存放的空间范围
    }
    
    //3.判断剩余的数据空间是不是还能够接受一次，不够的话主动回卷 从缓冲区的头开始接收
    if((U1_RxBuff_Size - U1_CB.U_Data_counter) < U1_RxMaxSize)
    {
        U1_CB.U_Data_IN->start = U1_RxBuff; // 将接收指针指向接收数组的开头
        U1_CB.U_Data_counter = 0; // 回卷，将接收数据计数清零
    }
    else // 如果剩余的数据空间大于等于一次接收的数据长度
    {
        U1_CB.U_Data_IN->start= &U1_RxBuff[U1_CB.U_Data_counter]; 
    }
    
    //4. 重新设置传输地址然后再次开启DMA
    DMA_Cmd(DMA1_Channel5, DISABLE); // 开启DMA通道
    DMA_ClearFlag(DMA1_FLAG_TC5); // 清除DMA传输完成标志
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(U1_CB.U_Data_IN->start); //DMA 通道的内存基地址
    DMA_Init(DMA1_Channel5, &DMA_InitStructure); //DMA 通道初始化
    DMA_SetCurrDataCounter(DMA1_Channel5, U1_RxMaxSize);
    DMA_Cmd(DMA1_Channel5, ENABLE); // 开启DMA通道
  } 
}




/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
